<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>datetimeformat.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_i18n_datetimeformat.js.html">datetimeformat.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Functions for dealing with date/time formatting.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>
<a name="line20"></a>/**
<a name="line21"></a> * Namespace for i18n date/time formatting functions
<a name="line22"></a> */
<a name="line23"></a>goog.provide(&#39;goog.i18n.DateTimeFormat&#39;);
<a name="line24"></a>goog.provide(&#39;goog.i18n.DateTimeFormat.Format&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line27"></a>goog.require(&#39;goog.date.DateLike&#39;);
<a name="line28"></a>goog.require(&#39;goog.i18n.DateTimeSymbols&#39;);
<a name="line29"></a>goog.require(&#39;goog.i18n.TimeZone&#39;);
<a name="line30"></a>goog.require(&#39;goog.string&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Datetime formatting functions following the pattern specification as defined
<a name="line35"></a> * in JDK, ICU and CLDR, with minor modification for typical usage in JS.
<a name="line36"></a> * Pattern specification: (Refer to JDK/ICU/CLDR)
<a name="line37"></a> * &lt;pre&gt;
<a name="line38"></a> * Symbol Meaning Presentation        Example
<a name="line39"></a> * ------   -------                 ------------        -------
<a name="line40"></a> * G        era designator          (Text)              AD
<a name="line41"></a> * y#       year                    (Number)            1996
<a name="line42"></a> * Y*       year (week of year)     (Number)            1997
<a name="line43"></a> * u*       extended year           (Number)            4601
<a name="line44"></a> * M        month in year           (Text &amp; Number)     July &amp; 07
<a name="line45"></a> * d        day in month            (Number)            10
<a name="line46"></a> * h        hour in am/pm (1~12)    (Number)            12
<a name="line47"></a> * H        hour in day (0~23)      (Number)            0
<a name="line48"></a> * m        minute in hour          (Number)            30
<a name="line49"></a> * s        second in minute        (Number)            55
<a name="line50"></a> * S        fractional second       (Number)            978
<a name="line51"></a> * E        day of week             (Text)              Tuesday
<a name="line52"></a> * e*       day of week (local 1~7) (Number)            2
<a name="line53"></a> * D*       day in year             (Number)            189
<a name="line54"></a> * F*       day of week in month    (Number)            2 (2nd Wed in July)
<a name="line55"></a> * w*       week in year            (Number)            27
<a name="line56"></a> * W*       week in month           (Number)            2
<a name="line57"></a> * a        am/pm marker            (Text)              PM
<a name="line58"></a> * k        hour in day (1~24)      (Number)            24
<a name="line59"></a> * K        hour in am/pm (0~11)    (Number)            0
<a name="line60"></a> * z        time zone               (Text)              Pacific Standard Time
<a name="line61"></a> * Z        time zone (RFC 822)     (Number)            -0800
<a name="line62"></a> * v        time zone (generic)     (Text)              Pacific Time
<a name="line63"></a> * g*       Julian day              (Number)            2451334
<a name="line64"></a> * A*       milliseconds in day     (Number)            69540000
<a name="line65"></a> * &#39;        escape for text         (Delimiter)         &#39;Date=&#39;
<a name="line66"></a> * &#39;&#39;       single quote            (Literal)           &#39;o&#39;&#39;clock&#39;
<a name="line67"></a> *
<a name="line68"></a> * Item marked with &#39;*&#39; are not supported yet.
<a name="line69"></a> * Item marked with &#39;#&#39; works different than java
<a name="line70"></a> *
<a name="line71"></a> * The count of pattern letters determine the format.
<a name="line72"></a> * (Text): 4 or more, use full form, &lt;4, use short or abbreviated form if it
<a name="line73"></a> * exists. (e.g., &quot;EEEE&quot; produces &quot;Monday&quot;, &quot;EEE&quot; produces &quot;Mon&quot;)
<a name="line74"></a> *
<a name="line75"></a> * (Number): the minimum number of digits. Shorter numbers are zero-padded to
<a name="line76"></a> * this amount (e.g. if &quot;m&quot; produces &quot;6&quot;, &quot;mm&quot; produces &quot;06&quot;). Year is handled
<a name="line77"></a> * specially; that is, if the count of &#39;y&#39; is 2, the Year will be truncated to
<a name="line78"></a> * 2 digits. (e.g., if &quot;yyyy&quot; produces &quot;1997&quot;, &quot;yy&quot; produces &quot;97&quot;.) Unlike other
<a name="line79"></a> * fields, fractional seconds are padded on the right with zero.
<a name="line80"></a> *
<a name="line81"></a> * (Text &amp; Number): 3 or over, use text, otherwise use number. (e.g., &quot;M&quot;
<a name="line82"></a> * produces &quot;1&quot;, &quot;MM&quot; produces &quot;01&quot;, &quot;MMM&quot; produces &quot;Jan&quot;, and &quot;MMMM&quot; produces
<a name="line83"></a> * &quot;January&quot;.)
<a name="line84"></a> *
<a name="line85"></a> * Any characters in the pattern that are not in the ranges of [&#39;a&#39;..&#39;z&#39;] and
<a name="line86"></a> * [&#39;A&#39;..&#39;Z&#39;] will be treated as quoted text. For instance, characters like &#39;:&#39;,
<a name="line87"></a> * &#39;.&#39;, &#39; &#39;, &#39;#&#39; and &#39;@&#39; will appear in the resulting time text even they are
<a name="line88"></a> * not embraced within single quotes.
<a name="line89"></a> * &lt;/pre&gt;
<a name="line90"></a> */
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Construct a DateTimeFormat object based on current locale.
<a name="line96"></a> * @constructor
<a name="line97"></a> * @param {string|number} pattern pattern specification or pattern type.
<a name="line98"></a> */
<a name="line99"></a>goog.i18n.DateTimeFormat = function(pattern) {
<a name="line100"></a>  goog.asserts.assert(goog.isDef(pattern), &#39;Pattern must be defined&#39;);
<a name="line101"></a>  this.patternParts_ = [];
<a name="line102"></a>  if (typeof pattern == &#39;number&#39;) {
<a name="line103"></a>    this.applyStandardPattern_(pattern);
<a name="line104"></a>  } else {
<a name="line105"></a>    this.applyPattern_(pattern);
<a name="line106"></a>  }
<a name="line107"></a>};
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>/**
<a name="line111"></a> * Enum to identify predefined Date/Time format pattern.
<a name="line112"></a> * @enum {number}
<a name="line113"></a> */
<a name="line114"></a>goog.i18n.DateTimeFormat.Format = {
<a name="line115"></a>  FULL_DATE: 0,
<a name="line116"></a>  LONG_DATE: 1,
<a name="line117"></a>  MEDIUM_DATE: 2,
<a name="line118"></a>  SHORT_DATE: 3,
<a name="line119"></a>  FULL_TIME: 4,
<a name="line120"></a>  LONG_TIME: 5,
<a name="line121"></a>  MEDIUM_TIME: 6,
<a name="line122"></a>  SHORT_TIME: 7,
<a name="line123"></a>  FULL_DATETIME: 8,
<a name="line124"></a>  LONG_DATETIME: 9,
<a name="line125"></a>  MEDIUM_DATETIME: 10,
<a name="line126"></a>  SHORT_DATETIME: 11
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * regular expression pattern for parsing pattern string
<a name="line132"></a> * @type {Array.&lt;RegExp&gt;}
<a name="line133"></a> * @private
<a name="line134"></a> */
<a name="line135"></a>goog.i18n.DateTimeFormat.TOKENS_ = [
<a name="line136"></a>  //quote string
<a name="line137"></a>  /^\&#39;(?:[^\&#39;]|\&#39;\&#39;)*\&#39;/,
<a name="line138"></a>  // pattern chars
<a name="line139"></a>  /^(?:G+|y+|M+|k+|S+|E+|a+|h+|K+|H+|c+|L+|Q+|d+|m+|s+|v+|z+|Z+)/,
<a name="line140"></a>  // and all the other chars
<a name="line141"></a>  /^[^\&#39;GyMkSEahKHcLQdmsvzZ]+/  // and all the other chars
<a name="line142"></a>];
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * These are token types, corresponding to above token definitions.
<a name="line147"></a> * @enum {number}
<a name="line148"></a> * @private
<a name="line149"></a> */
<a name="line150"></a>goog.i18n.DateTimeFormat.PartTypes_ = {
<a name="line151"></a>  QUOTED_STRING: 0,
<a name="line152"></a>  FIELD: 1,
<a name="line153"></a>  LITERAL: 2
<a name="line154"></a>};
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>/**
<a name="line158"></a> * Apply specified pattern to this formatter object.
<a name="line159"></a> * @param {string} pattern String specifying how the date should be formatted.
<a name="line160"></a> * @private
<a name="line161"></a> */
<a name="line162"></a>goog.i18n.DateTimeFormat.prototype.applyPattern_ = function(pattern) {
<a name="line163"></a>  // lex the pattern, once for all uses
<a name="line164"></a>  while (pattern) {
<a name="line165"></a>    for (var i = 0; i &lt; goog.i18n.DateTimeFormat.TOKENS_.length; ++i) {
<a name="line166"></a>      var m = pattern.match(goog.i18n.DateTimeFormat.TOKENS_[i]);
<a name="line167"></a>      if (m) {
<a name="line168"></a>        var part = m[0];
<a name="line169"></a>        pattern = pattern.substring(part.length);
<a name="line170"></a>        if (i == goog.i18n.DateTimeFormat.PartTypes_.QUOTED_STRING) {
<a name="line171"></a>          if (part == &quot;&#39;&#39;&quot;) {
<a name="line172"></a>            part = &quot;&#39;&quot;;  // &#39;&#39; -&gt; &#39;
<a name="line173"></a>          } else {
<a name="line174"></a>            part = part.substring(1, part.length - 1); // strip quotes
<a name="line175"></a>            part = part.replace(/\&#39;\&#39;/, &quot;&#39;&quot;);
<a name="line176"></a>          }
<a name="line177"></a>        }
<a name="line178"></a>        this.patternParts_.push({ text: part, type: i });
<a name="line179"></a>        break;
<a name="line180"></a>      }
<a name="line181"></a>    }
<a name="line182"></a>  }
<a name="line183"></a>};
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Format the given date object according to preset pattern and current lcoale.
<a name="line188"></a> * @param {goog.date.DateLike} date The Date object that is being formatted.
<a name="line189"></a> * @param {goog.i18n.TimeZone=} opt_timeZone optional, if specified, time
<a name="line190"></a> *    related fields will be formatted based on its setting. When this field
<a name="line191"></a> *    is not specified, &quot;undefined&quot; will be pass around and those function
<a name="line192"></a> *    that really need time zone service will create a default one.
<a name="line193"></a> * @return {string} Formatted string for the given date.
<a name="line194"></a> */
<a name="line195"></a>goog.i18n.DateTimeFormat.prototype.format = function(date, opt_timeZone) {
<a name="line196"></a>  // We don&#39;t want to write code to calculate each date field because we
<a name="line197"></a>  // want to maximize performance and minimize code size.
<a name="line198"></a>  // JavaScript only provide API to render local time.
<a name="line199"></a>  // Suppose target date is: 16:00 GMT-0400
<a name="line200"></a>  // OS local time is:       12:00 GMT-0800
<a name="line201"></a>  // We want to create a Local Date Object : 16:00 GMT-0800, and fix the
<a name="line202"></a>  // time zone display ourselves.
<a name="line203"></a>  // Thing get a little bit tricky when daylight time transition happens. For
<a name="line204"></a>  // example, suppose OS timeZone is America/Los_Angeles, it is impossible to
<a name="line205"></a>  // represent &quot;2006/4/2 02:30&quot; even for those timeZone that has no transition
<a name="line206"></a>  // at this time. Because 2:00 to 3:00 on that day does not exising in
<a name="line207"></a>  // America/Los_Angeles time zone. To avoid calculating date field through
<a name="line208"></a>  // our own code, we uses 3 Date object instead, one for &quot;Year, month, day&quot;,
<a name="line209"></a>  // one for time within that day, and one for timeZone object since it need
<a name="line210"></a>  // the real time to figure out actual time zone offset.
<a name="line211"></a>  var diff = opt_timeZone ?
<a name="line212"></a>      (date.getTimezoneOffset() - opt_timeZone.getOffset(date)) * 60000 : 0;
<a name="line213"></a>  var dateForDate = diff ? new Date(date.getTime() + diff) : date;
<a name="line214"></a>  var dateForTime = dateForDate;
<a name="line215"></a>  // in daylight time switch on/off hour, diff adjustment could alter time
<a name="line216"></a>  // because of timeZone offset change, move 1 day forward or backward.
<a name="line217"></a>  if (opt_timeZone &amp;&amp;
<a name="line218"></a>      dateForDate.getTimezoneOffset() != date.getTimezoneOffset()) {
<a name="line219"></a>    diff += diff &gt; 0 ? -24 * 60 * 60000 : 24 * 60 * 60000;
<a name="line220"></a>    dateForTime = new Date(date.getTime() + diff);
<a name="line221"></a>  }
<a name="line222"></a>
<a name="line223"></a>  var out = [];
<a name="line224"></a>  for (var i = 0; i &lt; this.patternParts_.length; ++i) {
<a name="line225"></a>    var text = this.patternParts_[i].text;
<a name="line226"></a>    if (goog.i18n.DateTimeFormat.PartTypes_.FIELD ==
<a name="line227"></a>        this.patternParts_[i].type) {
<a name="line228"></a>      out.push(this.formatField_(text, date, dateForDate, dateForTime,
<a name="line229"></a>                                 opt_timeZone));
<a name="line230"></a>    } else {
<a name="line231"></a>      out.push(text);
<a name="line232"></a>    }
<a name="line233"></a>  }
<a name="line234"></a>  return out.join(&#39;&#39;);
<a name="line235"></a>};
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * Apply a predefined pattern as identified by formatType, which is stored in
<a name="line240"></a> * locale specific repository.
<a name="line241"></a> * @param {number} formatType A number that identified the predefined pattern.
<a name="line242"></a> * @private
<a name="line243"></a> */
<a name="line244"></a>goog.i18n.DateTimeFormat.prototype.applyStandardPattern_ =
<a name="line245"></a>    function(formatType) {
<a name="line246"></a>  var pattern;
<a name="line247"></a>  if (formatType &lt; 4) {
<a name="line248"></a>    pattern = goog.i18n.DateTimeSymbols.DATEFORMATS[formatType];
<a name="line249"></a>  } else if (formatType &lt; 8) {
<a name="line250"></a>    pattern = goog.i18n.DateTimeSymbols.TIMEFORMATS[formatType - 4];
<a name="line251"></a>  } else if (formatType &lt; 12) {
<a name="line252"></a>    pattern = goog.i18n.DateTimeSymbols.DATEFORMATS[formatType - 8] +
<a name="line253"></a>              &#39; &#39; + goog.i18n.DateTimeSymbols.TIMEFORMATS[formatType - 8];
<a name="line254"></a>  } else {
<a name="line255"></a>    this.applyStandardPattern_(goog.i18n.DateTimeFormat.Format.MEDIUM_DATETIME);
<a name="line256"></a>    return;
<a name="line257"></a>  }
<a name="line258"></a>  this.applyPattern_(pattern);
<a name="line259"></a>};
<a name="line260"></a>
<a name="line261"></a>
<a name="line262"></a>/**
<a name="line263"></a> * Localizes a string potentially containing numbers, replacing ASCII digits
<a name="line264"></a> * with native digits if specified so by the locale. Leaves other characters.
<a name="line265"></a> *
<a name="line266"></a> * @param {string} input the string to be localized, using ASCII digits.
<a name="line267"></a> * @return {string} localized string, potentially using native digits.
<a name="line268"></a> * @private
<a name="line269"></a> */
<a name="line270"></a>goog.i18n.DateTimeFormat.prototype.localizeNumbers_ = function(input) {
<a name="line271"></a>  if (goog.i18n.DateTimeSymbols.ZERODIGIT === undefined) {
<a name="line272"></a>    return input;
<a name="line273"></a>  }
<a name="line274"></a>
<a name="line275"></a>  var parts = [];
<a name="line276"></a>  for (var i = 0; i &lt; input.length; i++) {
<a name="line277"></a>    var c = input.charCodeAt(i);
<a name="line278"></a>    parts.push((0x30 &lt;= c &amp;&amp; c &lt;= 0x39) ? // &#39;0&#39; &lt;= c &lt;= &#39;9&#39;
<a name="line279"></a>        String.fromCharCode(goog.i18n.DateTimeSymbols.ZERODIGIT + c - 0x30) :
<a name="line280"></a>        input.charAt(i));
<a name="line281"></a>  }
<a name="line282"></a>  return parts.join(&#39;&#39;);
<a name="line283"></a>};
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>/**
<a name="line287"></a> * Formats Era field according to pattern specified.
<a name="line288"></a> *
<a name="line289"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line290"></a> *     how a field should be formatted.
<a name="line291"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line292"></a> * @return {string} Formatted string that represent this field.
<a name="line293"></a> * @private
<a name="line294"></a> */
<a name="line295"></a>goog.i18n.DateTimeFormat.prototype.formatEra_ = function(count, date) {
<a name="line296"></a>  var value = date.getFullYear() &gt; 0 ? 1 : 0;
<a name="line297"></a>  return count &gt;= 4 ? goog.i18n.DateTimeSymbols.ERANAMES[value] :
<a name="line298"></a>                      goog.i18n.DateTimeSymbols.ERAS[value];
<a name="line299"></a>};
<a name="line300"></a>
<a name="line301"></a>
<a name="line302"></a>/**
<a name="line303"></a> * Formats Year field according to pattern specified
<a name="line304"></a> *   Javascript Date object seems incapable handling 1BC and
<a name="line305"></a> *   year before. It can show you year 0 which does not exists.
<a name="line306"></a> *   following we just keep consistent with javascript&#39;s
<a name="line307"></a> *   toString method. But keep in mind those things should be
<a name="line308"></a> *   unsupported.
<a name="line309"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line310"></a> *     how a field should be formatted.
<a name="line311"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line312"></a> * @return {string} Formatted string that represent this field.
<a name="line313"></a> * @private
<a name="line314"></a> */
<a name="line315"></a>goog.i18n.DateTimeFormat.prototype.formatYear_ = function(count, date) {
<a name="line316"></a>  var value = date.getFullYear();
<a name="line317"></a>  if (value &lt; 0) {
<a name="line318"></a>    value = -value;
<a name="line319"></a>  }
<a name="line320"></a>  return this.localizeNumbers_(count == 2 ?
<a name="line321"></a>      goog.string.padNumber(value % 100, 2) :
<a name="line322"></a>      String(value));
<a name="line323"></a>};
<a name="line324"></a>
<a name="line325"></a>
<a name="line326"></a>/**
<a name="line327"></a> * Formats Month field according to pattern specified
<a name="line328"></a> *
<a name="line329"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line330"></a> *     how a field should be formatted.
<a name="line331"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line332"></a> * @return {string} Formatted string that represent this field.
<a name="line333"></a> * @private
<a name="line334"></a> */
<a name="line335"></a>goog.i18n.DateTimeFormat.prototype.formatMonth_ = function(count, date) {
<a name="line336"></a>  var value = date.getMonth();
<a name="line337"></a>  switch (count) {
<a name="line338"></a>    case 5: return goog.i18n.DateTimeSymbols.NARROWMONTHS[value];
<a name="line339"></a>    case 4: return goog.i18n.DateTimeSymbols.MONTHS[value];
<a name="line340"></a>    case 3: return goog.i18n.DateTimeSymbols.SHORTMONTHS[value];
<a name="line341"></a>    default:
<a name="line342"></a>      return this.localizeNumbers_(goog.string.padNumber(value + 1, count));
<a name="line343"></a>  }
<a name="line344"></a>};
<a name="line345"></a>
<a name="line346"></a>
<a name="line347"></a>/**
<a name="line348"></a> * Formats (1..24) Hours field according to pattern specified
<a name="line349"></a> *
<a name="line350"></a> * @param {number} count Number of time pattern char repeats. This controls
<a name="line351"></a> *     how a field should be formatted.
<a name="line352"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line353"></a> * @return {string} Formatted string that represent this field.
<a name="line354"></a> * @private
<a name="line355"></a> */
<a name="line356"></a>goog.i18n.DateTimeFormat.prototype.format24Hours_ =
<a name="line357"></a>    function(count, date) {
<a name="line358"></a>  return this.localizeNumbers_(
<a name="line359"></a>      goog.string.padNumber(date.getHours() || 24, count));
<a name="line360"></a>};
<a name="line361"></a>
<a name="line362"></a>
<a name="line363"></a>/**
<a name="line364"></a> * Formats Fractional seconds field according to pattern
<a name="line365"></a> * specified
<a name="line366"></a> *
<a name="line367"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line368"></a> *     how a field should be formatted.
<a name="line369"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line370"></a> *
<a name="line371"></a> * @return {string} Formatted string that represent this field.
<a name="line372"></a> * @private
<a name="line373"></a> */
<a name="line374"></a>goog.i18n.DateTimeFormat.prototype.formatFractionalSeconds_ =
<a name="line375"></a>    function(count, date) {
<a name="line376"></a>  // Fractional seconds left-justify, append 0 for precision beyond 3
<a name="line377"></a>  var value = date.getTime() % 1000 / 1000;
<a name="line378"></a>  return this.localizeNumbers_(
<a name="line379"></a>      value.toFixed(Math.min(3, count)).substr(2) +
<a name="line380"></a>      (count &gt; 3 ? goog.string.padNumber(0, count - 3) : &#39;&#39;));
<a name="line381"></a>};
<a name="line382"></a>
<a name="line383"></a>
<a name="line384"></a>/**
<a name="line385"></a> * Formats Day of week field according to pattern specified
<a name="line386"></a> *
<a name="line387"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line388"></a> *     how a field should be formatted.
<a name="line389"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line390"></a> * @return {string} Formatted string that represent this field.
<a name="line391"></a> * @private
<a name="line392"></a> */
<a name="line393"></a>goog.i18n.DateTimeFormat.prototype.formatDayOfWeek_ =
<a name="line394"></a>    function(count, date) {
<a name="line395"></a>  var value = date.getDay();
<a name="line396"></a>  return count &gt;= 4 ? goog.i18n.DateTimeSymbols.WEEKDAYS[value] :
<a name="line397"></a>                      goog.i18n.DateTimeSymbols.SHORTWEEKDAYS[value];
<a name="line398"></a>};
<a name="line399"></a>
<a name="line400"></a>
<a name="line401"></a>/**
<a name="line402"></a> * Formats Am/Pm field according to pattern specified
<a name="line403"></a> *
<a name="line404"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line405"></a> *     how a field should be formatted.
<a name="line406"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line407"></a> * @return {string} Formatted string that represent this field.
<a name="line408"></a> * @private
<a name="line409"></a> */
<a name="line410"></a>goog.i18n.DateTimeFormat.prototype.formatAmPm_ = function(count, date) {
<a name="line411"></a>  var hours = date.getHours();
<a name="line412"></a>  return goog.i18n.DateTimeSymbols.AMPMS[hours &gt;= 12 &amp;&amp; hours &lt; 24 ? 1 : 0];
<a name="line413"></a>};
<a name="line414"></a>
<a name="line415"></a>
<a name="line416"></a>/**
<a name="line417"></a> * Formats (1..12) Hours field according to pattern specified
<a name="line418"></a> *
<a name="line419"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line420"></a> *     how a field should be formatted.
<a name="line421"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line422"></a> * @return {string} formatted string that represent this field.
<a name="line423"></a> * @private
<a name="line424"></a> */
<a name="line425"></a>goog.i18n.DateTimeFormat.prototype.format1To12Hours_ =
<a name="line426"></a>    function(count, date) {
<a name="line427"></a>  return this.localizeNumbers_(
<a name="line428"></a>      goog.string.padNumber(date.getHours() % 12 || 12, count));
<a name="line429"></a>};
<a name="line430"></a>
<a name="line431"></a>
<a name="line432"></a>/**
<a name="line433"></a> * Formats (0..11) Hours field according to pattern specified
<a name="line434"></a> *
<a name="line435"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line436"></a> *     how a field should be formatted.
<a name="line437"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line438"></a> * @return {string} formatted string that represent this field.
<a name="line439"></a> * @private
<a name="line440"></a> */
<a name="line441"></a>goog.i18n.DateTimeFormat.prototype.format0To11Hours_ =
<a name="line442"></a>    function(count, date) {
<a name="line443"></a>  return this.localizeNumbers_(
<a name="line444"></a>      goog.string.padNumber(date.getHours() % 12, count));
<a name="line445"></a>};
<a name="line446"></a>
<a name="line447"></a>
<a name="line448"></a>/**
<a name="line449"></a> * Formats (0..23) Hours field according to pattern specified
<a name="line450"></a> *
<a name="line451"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line452"></a> *     how a field should be formatted.
<a name="line453"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line454"></a> * @return {string} formatted string that represent this field.
<a name="line455"></a> * @private
<a name="line456"></a> */
<a name="line457"></a>goog.i18n.DateTimeFormat.prototype.format0To23Hours_ =
<a name="line458"></a>    function(count, date) {
<a name="line459"></a>  return this.localizeNumbers_(goog.string.padNumber(date.getHours(), count));
<a name="line460"></a>};
<a name="line461"></a>
<a name="line462"></a>
<a name="line463"></a>/**
<a name="line464"></a> * Formats Standalone weekday field according to pattern specified
<a name="line465"></a> *
<a name="line466"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line467"></a> *     how a field should be formatted.
<a name="line468"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line469"></a> * @return {string} formatted string that represent this field.
<a name="line470"></a> * @private
<a name="line471"></a> */
<a name="line472"></a>goog.i18n.DateTimeFormat.prototype.formatStandaloneDay_ =
<a name="line473"></a>    function(count, date) {
<a name="line474"></a>  var value = date.getDay();
<a name="line475"></a>  switch (count) {
<a name="line476"></a>    case 5:
<a name="line477"></a>      return goog.i18n.DateTimeSymbols.STANDALONENARROWWEEKDAYS[value];
<a name="line478"></a>    case 4:
<a name="line479"></a>      return goog.i18n.DateTimeSymbols.STANDALONEWEEKDAYS[value];
<a name="line480"></a>    case 3:
<a name="line481"></a>      return goog.i18n.DateTimeSymbols.STANDALONESHORTWEEKDAYS[value];
<a name="line482"></a>    default:
<a name="line483"></a>      return this.localizeNumbers_(goog.string.padNumber(value, 1));
<a name="line484"></a>  }
<a name="line485"></a>};
<a name="line486"></a>
<a name="line487"></a>
<a name="line488"></a>/**
<a name="line489"></a> * Formats Standalone Month field according to pattern specified
<a name="line490"></a> *
<a name="line491"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line492"></a> *     how a field should be formatted.
<a name="line493"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line494"></a> * @return {string} formatted string that represent this field.
<a name="line495"></a> * @private
<a name="line496"></a> */
<a name="line497"></a>goog.i18n.DateTimeFormat.prototype.formatStandaloneMonth_ =
<a name="line498"></a>    function(count, date) {
<a name="line499"></a>  var value = date.getMonth();
<a name="line500"></a>  switch (count) {
<a name="line501"></a>    case 5:
<a name="line502"></a>      return goog.i18n.DateTimeSymbols.STANDALONENARROWMONTHS[value];
<a name="line503"></a>    case 4:
<a name="line504"></a>      return goog.i18n.DateTimeSymbols.STANDALONEMONTHS[value];
<a name="line505"></a>    case 3:
<a name="line506"></a>      return goog.i18n.DateTimeSymbols.STANDALONESHORTMONTHS[value];
<a name="line507"></a>    default:
<a name="line508"></a>      return this.localizeNumbers_(goog.string.padNumber(value + 1, count));
<a name="line509"></a>  }
<a name="line510"></a>};
<a name="line511"></a>
<a name="line512"></a>
<a name="line513"></a>/**
<a name="line514"></a> * Formats Quarter field according to pattern specified
<a name="line515"></a> *
<a name="line516"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line517"></a> *     how a field should be formatted.
<a name="line518"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line519"></a> * @return {string} Formatted string that represent this field.
<a name="line520"></a> * @private
<a name="line521"></a> */
<a name="line522"></a>goog.i18n.DateTimeFormat.prototype.formatQuarter_ =
<a name="line523"></a>    function(count, date) {
<a name="line524"></a>  var value = Math.floor(date.getMonth() / 3);
<a name="line525"></a>  return count &lt; 4 ? goog.i18n.DateTimeSymbols.SHORTQUARTERS[value] :
<a name="line526"></a>                     goog.i18n.DateTimeSymbols.QUARTERS[value];
<a name="line527"></a>};
<a name="line528"></a>
<a name="line529"></a>
<a name="line530"></a>/**
<a name="line531"></a> * Formats Date field according to pattern specified
<a name="line532"></a> *
<a name="line533"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line534"></a> *     how a field should be formatted.
<a name="line535"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line536"></a> * @return {string} Formatted string that represent this field.
<a name="line537"></a> * @private
<a name="line538"></a> */
<a name="line539"></a>goog.i18n.DateTimeFormat.prototype.formatDate_ = function(count, date) {
<a name="line540"></a>  return this.localizeNumbers_(goog.string.padNumber(date.getDate(), count));
<a name="line541"></a>};
<a name="line542"></a>
<a name="line543"></a>
<a name="line544"></a>/**
<a name="line545"></a> * Formats Minutes field according to pattern specified
<a name="line546"></a> *
<a name="line547"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line548"></a> *     how a field should be formatted.
<a name="line549"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line550"></a> * @return {string} Formatted string that represent this field.
<a name="line551"></a> * @private
<a name="line552"></a> */
<a name="line553"></a>goog.i18n.DateTimeFormat.prototype.formatMinutes_ =
<a name="line554"></a>    function(count, date) {
<a name="line555"></a>  return this.localizeNumbers_(goog.string.padNumber(date.getMinutes(), count));
<a name="line556"></a>};
<a name="line557"></a>
<a name="line558"></a>
<a name="line559"></a>/**
<a name="line560"></a> * Formats Seconds field according to pattern specified
<a name="line561"></a> *
<a name="line562"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line563"></a> *     how a field should be formatted.
<a name="line564"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line565"></a> * @return {string} Formatted string that represent this field.
<a name="line566"></a> * @private
<a name="line567"></a> */
<a name="line568"></a>goog.i18n.DateTimeFormat.prototype.formatSeconds_ =
<a name="line569"></a>    function(count, date) {
<a name="line570"></a>  return this.localizeNumbers_(goog.string.padNumber(date.getSeconds(), count));
<a name="line571"></a>};
<a name="line572"></a>
<a name="line573"></a>
<a name="line574"></a>/**
<a name="line575"></a> * Formats TimeZone field following RFC
<a name="line576"></a> *
<a name="line577"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line578"></a> *     how a field should be formatted.
<a name="line579"></a> * @param {goog.date.DateLike} date It holds the date object to be formatted.
<a name="line580"></a> * @param {goog.i18n.TimeZone=} opt_timeZone This holds current time zone info.
<a name="line581"></a> * @return {string} Formatted string that represent this field.
<a name="line582"></a> * @private
<a name="line583"></a> */
<a name="line584"></a>goog.i18n.DateTimeFormat.prototype.formatTimeZoneRFC_ =
<a name="line585"></a>    function(count, date, opt_timeZone) {
<a name="line586"></a>  opt_timeZone = opt_timeZone ||
<a name="line587"></a>      goog.i18n.TimeZone.createTimeZone(date.getTimezoneOffset());
<a name="line588"></a>
<a name="line589"></a>  // RFC 822 formats should be kept in ASCII, but localized GMT formats may need
<a name="line590"></a>  // to use native digits.
<a name="line591"></a>  return count &lt; 4 ? opt_timeZone.getRFCTimeZoneString(date) :
<a name="line592"></a>                     this.localizeNumbers_(opt_timeZone.getGMTString(date));
<a name="line593"></a>};
<a name="line594"></a>
<a name="line595"></a>
<a name="line596"></a>/**
<a name="line597"></a> * Generate GMT timeZone string for given date
<a name="line598"></a> * @param {number} count Number of time pattern char repeats, it controls
<a name="line599"></a> *     how a field should be formatted.
<a name="line600"></a> * @param {goog.date.DateLike} date Whose value being evaluated.
<a name="line601"></a> * @param {goog.i18n.TimeZone=} opt_timeZone This holds current time zone info.
<a name="line602"></a> * @return {string} GMT timeZone string.
<a name="line603"></a> * @private
<a name="line604"></a> */
<a name="line605"></a>goog.i18n.DateTimeFormat.prototype.formatTimeZone_ =
<a name="line606"></a>    function(count, date, opt_timeZone) {
<a name="line607"></a>  opt_timeZone = opt_timeZone ||
<a name="line608"></a>      goog.i18n.TimeZone.createTimeZone(date.getTimezoneOffset());
<a name="line609"></a>  return count &lt; 4 ? opt_timeZone.getShortName(date) :
<a name="line610"></a>             opt_timeZone.getLongName(date);
<a name="line611"></a>};
<a name="line612"></a>
<a name="line613"></a>
<a name="line614"></a>/**
<a name="line615"></a> * Generate GMT timeZone string for given date
<a name="line616"></a> * @param {goog.date.DateLike} date Whose value being evaluated.
<a name="line617"></a> * @param {goog.i18n.TimeZone=} opt_timeZone This holds current time zone info.
<a name="line618"></a> * @return {string} GMT timeZone string.
<a name="line619"></a> * @private
<a name="line620"></a> */
<a name="line621"></a>goog.i18n.DateTimeFormat.prototype.formatTimeZoneId_ =
<a name="line622"></a>    function(date, opt_timeZone) {
<a name="line623"></a>  opt_timeZone = opt_timeZone ||
<a name="line624"></a>      goog.i18n.TimeZone.createTimeZone(date.getTimezoneOffset());
<a name="line625"></a>  return opt_timeZone.getTimeZoneId();
<a name="line626"></a>};
<a name="line627"></a>
<a name="line628"></a>
<a name="line629"></a>/**
<a name="line630"></a> * Formatting one date field.
<a name="line631"></a> * @param {string} patternStr The pattern string for the field being formatted.
<a name="line632"></a> * @param {goog.date.DateLike} date represents the real date to be formatted.
<a name="line633"></a> * @param {goog.date.DateLike} dateForDate used to resolve date fields
<a name="line634"></a> *     for formatting.
<a name="line635"></a> * @param {goog.date.DateLike} dateForTime used to resolve time fields
<a name="line636"></a> *     for formatting.
<a name="line637"></a> * @param {goog.i18n.TimeZone=} opt_timeZone This holds current time zone info.
<a name="line638"></a> * @return {string} string representation for the given field.
<a name="line639"></a> * @private
<a name="line640"></a> */
<a name="line641"></a>goog.i18n.DateTimeFormat.prototype.formatField_ =
<a name="line642"></a>    function(patternStr, date, dateForDate, dateForTime, opt_timeZone) {
<a name="line643"></a>  var count = patternStr.length;
<a name="line644"></a>  switch (patternStr.charAt(0)) {
<a name="line645"></a>    case &#39;G&#39;: return this.formatEra_(count, dateForDate);
<a name="line646"></a>    case &#39;y&#39;: return this.formatYear_(count, dateForDate);
<a name="line647"></a>    case &#39;M&#39;: return this.formatMonth_(count, dateForDate);
<a name="line648"></a>    case &#39;k&#39;: return this.format24Hours_(count, dateForTime);
<a name="line649"></a>    case &#39;S&#39;: return this.formatFractionalSeconds_(count, dateForTime);
<a name="line650"></a>    case &#39;E&#39;: return this.formatDayOfWeek_(count, dateForDate);
<a name="line651"></a>    case &#39;a&#39;: return this.formatAmPm_(count, dateForTime);
<a name="line652"></a>    case &#39;h&#39;: return this.format1To12Hours_(count, dateForTime);
<a name="line653"></a>    case &#39;K&#39;: return this.format0To11Hours_(count, dateForTime);
<a name="line654"></a>    case &#39;H&#39;: return this.format0To23Hours_(count, dateForTime);
<a name="line655"></a>    case &#39;c&#39;: return this.formatStandaloneDay_(count, dateForDate);
<a name="line656"></a>    case &#39;L&#39;: return this.formatStandaloneMonth_(count, dateForDate);
<a name="line657"></a>    case &#39;Q&#39;: return this.formatQuarter_(count, dateForDate);
<a name="line658"></a>    case &#39;d&#39;: return this.formatDate_(count, dateForDate);
<a name="line659"></a>    case &#39;m&#39;: return this.formatMinutes_(count, dateForTime);
<a name="line660"></a>    case &#39;s&#39;: return this.formatSeconds_(count, dateForTime);
<a name="line661"></a>    case &#39;v&#39;: return this.formatTimeZoneId_(date, opt_timeZone);
<a name="line662"></a>    case &#39;z&#39;: return this.formatTimeZone_(count, date, opt_timeZone);
<a name="line663"></a>    case &#39;Z&#39;: return this.formatTimeZoneRFC_(count, date, opt_timeZone);
<a name="line664"></a>    default: return &#39;&#39;;
<a name="line665"></a>  }
<a name="line666"></a>};
</pre>


</body>
</html>
